package com.ruoyi.personnel.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.personnel.vo.UserRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.personnel.mapper.UserRegisterMapper;
import com.ruoyi.personnel.domain.UserRegister;
import com.ruoyi.personnel.service.IUserRegisterService;
import com.ruoyi.common.core.text.Convert;

/**
 * 用户注册Service业务层处理
 *
 * @author ruoyi
 * @date 2020-02-26
 */
@Service
public class UserRegisterServiceImpl implements IUserRegisterService {

    @Autowired
    private UserRegisterMapper userRegisterMapper;

    /**
     * 查询用户注册
     *
     * @param id 用户注册ID
     * @return 用户注册
     */
    @Override
    public UserRegister selectUserRegisterById(Long id) {
        return userRegisterMapper.selectUserRegisterById(id);
    }

    /**
     * 查询用户注册列表
     *
     * @param userRegister 用户注册
     * @return 用户注册
     */
    @Override
    public List<UserRegister> selectUserRegisterList(UserRegister userRegister) {
        return userRegisterMapper.selectUserRegisterList(userRegister);
    }

    /**
     * 新增用户注册
     *
     * @param userRegister 用户注册
     * @return 结果
     */
    @Override
    public int insertUserRegister(UserRegister userRegister) {
        return userRegisterMapper.insertUserRegister(userRegister);
    }

    /**
     * 修改用户注册
     *
     * @param userRegister 用户注册
     * @return 结果
     */
    @Override
    public int updateUserRegister(UserRegister userRegister) {
        return userRegisterMapper.updateUserRegister(userRegister);
    }

    /**
     * 删除用户注册对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserRegisterByIds(String ids) {
        return userRegisterMapper.deleteUserRegisterByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户注册信息
     *
     * @param id 用户注册ID
     * @return 结果
     */
    @Override
    public int deleteUserRegisterById(Long id) {
        return userRegisterMapper.deleteUserRegisterById(id);
    }


    /**
     * 用户注册或更新,存在更新
     *
     * @param userRegisterVO
     * @return 结果，受影响的行数
     */
    @Override
    public int userRegisterOrUpdate(UserRegisterVO userRegisterVO) {
        int count = 0;  //受影响的行数
        UserRegister userRegister = new UserRegister();
        //将VO类中的数据复制一份到实体类中，避免修改入参导致频繁修改实体类
        BeanUtils.copyBeanProp(userRegister, userRegisterVO);
        //根据OPENID,查询用户是否存在
        UserRegister user = userRegisterMapper.selectByOpenId(userRegisterVO.getOpenId());
        if (user != null) {  //更新
            count = userRegisterMapper.updateUser(userRegister);
        } else {  //注册
            count = userRegisterMapper.insertUser(userRegister);
        }
        return count;
    }
}
